package com.navinfo.platform.trip.analysis.service.impl.dongfeng;

import com.navinfo.platform.trip.common.annotation.SystemType;
import com.navinfo.platform.trip.common.enums.SystemTypeEnum;
import com.navinfo.platform.trip.analysis.service.TripJudgeService;
import com.navinfo.platform.trip.common.pojo.PointProtocol;
import com.navinfo.platform.trip.common.enums.TripStatusEnum;
import com.navinfo.platform.trip.analysis.pojo.TripJudgeState;
import org.apache.flink.api.java.utils.ParameterTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Optional;

import static com.navinfo.platform.trip.common.enums.TripStatusEnum.*;

/**
 * 东风行程切分服务
 * 行程第一个点：速度>0且acc=on：行程开始；否则，行程未开始；
 * 行程非第一个点：
 *      两点时差大于10分钟：行程强制结束，
 *      两点时差小于十分钟：（速度=0且acc=off 或 位置点个数>5000），行程结束；否则，行程进行中；
 *
 * @author: web
 * @data : 2020-04-07
 **/
@SystemType(value= SystemTypeEnum.DF, interfaceClass= TripJudgeService.class )
public class TripJudgeServiceImpl implements TripJudgeService {
    private static final Logger logger = LoggerFactory.getLogger(TripJudgeServiceImpl.class);

    /**
     * 速度阈值
     */
    private static final long SPEED_THRESHOLD = 0;
    /**
     * 0200位置点个数阈值
     */
    private static final long COUNT_THRESHOLD = 5000;

    /**
     * 最小速度阀值
     */
    private int speedMin, speedMax;
    /**
     * 引擎最小转速阀值
     */
    private int rotationMin,rotationMax;

    public TripJudgeServiceImpl(ParameterTool jobConfig) {
        rotationMin = jobConfig.getInt("rotation_min", 0);
        rotationMax = jobConfig.getInt("rotation_max", 10000);
        speedMin = jobConfig.getInt("speed_min", 0);
        speedMax = jobConfig.getInt("speed_max", 200);
    }

    @Override
    public SystemTypeEnum systemType() {
        return SystemTypeEnum.DF;
    }

    @Override
    public TripStatusEnum tripStatus(TripJudgeState tripJudgeState, PointProtocol point) {

        //行程未开始
        if (null==tripJudgeState || null==tripJudgeState.getLastGpsTime() ) {
            return (point.getAccStatus()==1 && point.getSpeedKmh()>SPEED_THRESHOLD) ? TRIP_START : TRIP_NO_START;
        }

        //行程已开始
        else {
            // 当前时间-上一个时间超过指定阀值，强制结束行程
            long currGpsTime = point.getGpsTime();
            if (isForceEnd(Optional.ofNullable(tripJudgeState.getLastGpsTime()).orElse(currGpsTime), currGpsTime)) {
                return TRIP_FORCE_END;
            } else {
                //行程结束：(acc=off && 速度 = 0) || 位置点个数>5000
                return ((point.getAccStatus()==0 && point.getSpeedKmh()==SPEED_THRESHOLD) || tripJudgeState.getCount0200()>COUNT_THRESHOLD) ? TRIP_END : TRIP_IN;
            }
        }
    }

    @Override
    public boolean validate(PointProtocol point, long lastGpsTime) {
        //本次gps时间小于等于上个点的gps时间，不予处理!
        if (point.getGpsTime() <= lastGpsTime) {
            return false;
        }

        //发动机转速不在合理区间内
        int rotation = point.getRotation();
        if (rotation<rotationMin || rotation>rotationMax) {
            return false;
        }

        //速度不在合理区间内
        double speedKmh = point.getSpeedKmh();
        if(speedKmh<speedMin || speedMax>200) {
            return false;
        }

        return true;
    }

}
